package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.provider.mapper.CustomerAppdepotMapper;
import com.weixing.mall.provider.model.domain.CustomerAppdepot;
import com.weixing.mall.provider.model.domain.Dict;
import com.weixing.mall.provider.model.dto.sys.CustomerAppdepotDto;
import com.weixing.mall.provider.model.query.CustomerAppdepotQuery;
import com.weixing.mall.provider.model.vo.sys.CustomerAppdepotVo;
import com.weixing.mall.provider.model.vo.sys.SysUserRoleVo;
import com.weixing.mall.provider.service.ICustomerAppdepotService;
import com.weixing.mall.provider.service.IDictService;
import com.weixing.mall.provider.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * APP仓库与客服人员的绑定关系 服务实现类
 * </p>
 *
 * @author Small
 * @since 2021-08-04
 */
@Service
public class CustomerAppdepotServiceImpl extends ServiceImpl<CustomerAppdepotMapper, CustomerAppdepot> implements ICustomerAppdepotService {

    @Autowired
    private IDictService sysDictService;
    @Autowired
    private IUserService userService;

    @Override
    public IPage<CustomerAppdepotVo> selectOfPage(Page initMpPage,CustomerAppdepotQuery customerAppdepotQuery) {
        return baseMapper.selectAllOfPage(initMpPage,customerAppdepotQuery);
    }

    @Override
    public void save(CustomerAppdepotDto customerAppdepotDto) {
        CustomerAppdepot customerAppdepot = new CustomerAppdepot();
        BeanUtils.copyProperties(customerAppdepotDto, customerAppdepot);
        customerAppdepot.setIsAvailable(customerAppdepotDto.getIsAvailable().getValue());
        if (customerAppdepotDto.getId() == null) {
            save(customerAppdepot);
        } else {
            updateById(customerAppdepot);
        }
    }


    /**
     * 根据角色集合返回人员列表
     *
     * @param code
     * @return
     */
    @Override
    public List<SysUserRoleVo> userListByCode(String code) {
        Dict dict = sysDictService.getOne(new QueryWrapper<Dict>().eq("dict_group_code", code));
        String[] roleCode = dict.getDictValueDesp().split(",");
        List<String> list = java.util.Arrays.asList(roleCode);
        List<SysUserRoleVo> result = userService.selectSysUserByRoleCode(list, WhetherEnum.YES.getValue());
        return result;
    }

    @Override
    public String checkRepeat(CustomerAppdepotDto customerAppdepotDto) {
        String msg=null;
        try {
            CustomerAppdepot customerAppdepot = getOne(new QueryWrapper<CustomerAppdepot>().eq("customer_id", customerAppdepotDto.getCustomerId()).eq("app_depot_id", customerAppdepotDto.getAppDepotId()).eq("is_available",WhetherEnum.YES.getValue()));
            if(customerAppdepotDto.getId()==null&&customerAppdepot!=null){
                return "已存在重复的绑定关系，请检查数据";
            }
            if(customerAppdepotDto.getId()!=null&&customerAppdepot!=null&&!customerAppdepotDto.getId().equals(customerAppdepot.getId())){//不为空，且和查询出来的内容不一致，则不允许保存
                return  "已存在重复的绑定关系，请检查数据";
            }
            customerAppdepot = getOne(new QueryWrapper<CustomerAppdepot>().eq("app_depot_id", customerAppdepotDto.getAppDepotId()).eq("is_available",WhetherEnum.YES.getValue()));
            if(null!=customerAppdepot){//该APP仓库人员已经绑定了客服
                if(customerAppdepotDto.getId()==null){//如果为新增，则不允许操作
                    return "已存在重复的绑定关系，请检查数据";
                }else{//如果为修改，则查询是否为可用，如果为可用，则不允许修改
                    if(customerAppdepotDto.getIsAvailable().getValue().intValue()==WhetherEnum.YES.getValue().intValue()){
                        return  "已存在重复的绑定关系，请检查数据";
                    }
                }

            }
        } catch (Exception e) {
            e.getMessage();
            return "已存在重复的绑定关系，请检查数据";
        }

        return msg;
    }
}
